package com.admgt.profile.dao;


import java.util.Calendar;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.admgt.profile.entity.Profile;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/app-config.xml"})
public class ProfileDAOTest {
	private ProfileDAO dao;
	@Autowired
	public void setDao(ProfileDAO dao){
		this.dao = dao;
	}
	@Test
	public void testCreateData(){
		int expectedResult = 2;
		Profile profile = new Profile();
		profile.setName("Barry Jhon");
		//profile.setCreateTime(new Date(System.currentTimeMillis()));
		profile.setCreateTime( Calendar.getInstance().getTime());
		dao.saveProfile(profile);
		
		Profile p2 = new Profile();
		p2.setName("Barry Jhon2");
		//profile.setCreateTime(new Date(System.currentTimeMillis()));
		p2.setCreateTime( Calendar.getInstance().getTime());
		dao.saveProfile(p2);
		
		List<Profile> list =dao.getAllProfiles(new Profile());
		int size =list!= null?list.size():0;
		Assert.assertEquals(expectedResult,size );
	}	
	@Test
	public void testRetriveData(){
		List<Profile> profileList = dao.getAllProfiles(new Profile());
		Assert.assertEquals(2, profileList.size());
		Profile profileExpected =  profileList.get(0);
		Profile profileResult = dao.getProfileById(profileExpected.getProfileId());
		System.out.println(profileResult);
		System.out.println(profileResult.getProfileId());
		Assert.assertEquals(profileExpected.getProfileId(),
				profileResult.getProfileId());
	}
	@Test
	public void voidTestUpdateData(){
		List<Profile> profileList = dao.getAllProfiles(new Profile());
		Assert.assertEquals(2,profileList.size());
		Profile expected = profileList.get(0);
		expected.setName("James Arlen");
		dao.saveProfile(expected);
		
		Profile result = dao.getProfileById(expected.getProfileId());
		System.out.println(result);
		Assert.assertEquals(expected.getName(),result.getName());
	}
	@Test
	public void testDeleteData(){
		List<Profile> profileList = dao.getAllProfiles(new Profile());
		Assert.assertEquals(2, profileList.size());
		Profile expected = profileList.get(0);
		dao.deleteProfile(expected);
		Profile result = dao.getProfileById(expected.getProfileId());
		
		Profile expected2 = profileList.get(1);
		dao.deleteProfile(expected2);
		Profile result2 = dao.getProfileById(expected2.getProfileId());
		
		Assert.assertEquals(result, null);
		Assert.assertEquals(result2, null);
	}
}
